package com.itheima.chapter06.controller;

import com.itheima.chapter06.domain.Comment;
import com.itheima.chapter06.service.ApiCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ApiCommentController {
    @Autowired
    private ApiCommentService commentService;

    @GetMapping("/get/{id}")
    public Comment findById(@PathVariable("id") int id) {
        Comment comment = commentService.findById(id);
        return comment;
    }

    @GetMapping("/update/{id}/{author}")
    public Comment updateComment(@PathVariable("id") int id,
                                 @PathVariable("author") String author) {
        Comment comment = commentService.findById(id);
        comment.setAuthor(author);
        return commentService.updateComment(comment);
    }

    @GetMapping("/delete/{id}")
    public void deleteComment(@PathVariable("id") int id) {
        commentService.deleteComment(id);
    }
}
